<?php
/**
 * \file core_exception.php
 * \author Bontiv
 * \brief CMS Exception
 * \version 0.1

 * This file content common exeptions of the CMS.

 * \defgroup exception CMS Exceptions
 * Group which content all CMS Exceptions
 */
 
 /**
  * \class CoreException
  * \ingroup exception
  * Default CMS exception.
  **/
class CoreException extends Exception {
	
	/// General error breck the CMS execution.
	protected $generalError = false;
	
	/// Library or module which send the exception.
	protected $library;
	
	/// Type of the exception (string).
	/// For exemple "Forbidden" for the 403 HTML error.
	protected $type;
	
	/// HTML number of the error page.
	/// For exemple 403 for forbidden.
	protected $header;
	
	/// \brief Constructor
	/// Create an exception
	/// \param $msg Messsage of the exception.
	/// \param $code internal code of the error.
	public function __construct($msg = '', $code = 0)
	{
		$this->message = $msg;
		$this->code = $code;
		$lib = debug_backtrace(false);
		//var_dump($lib);
	}
	
	/// Get an associatif array for display error template.
	public function getTplVar ()
	{
		if (isset($this->header) && $this->header != 0)
			header("HTTP/1.1 $this->header $this->type");
		return array (
			'err_general' => ($this->generalError)?('true'):('false'),
			'err_type'	=> $this->type,
			'err_library' => $this->library,
			'err_code' => $this->code,
			'err_msg' => $this->message,
			'err_lne' => $this->line,
			'err_fle' => $this->file,
		);
	}
}

/// \class LibraryLoadingError
/// \biref Library loading error.
/// \ingroup exception
///
/// Exception raise when a library can't be loaded.
class LibraryLoadingError extends CoreException {
	public function __construct($msg, $code = 0)
	{
		$this->library = 'Library Loader';
		$this->type = 'core';
		$this->generalError = true;
		$this->message = $msg;
		$this->code = $code;
	}
}

/// \class ModuleLoadingError
/// \brief Module loading error.
/// \ingroup exception
///
/// Exception raise when a module can't be loaded.
class ModuleLoadingError extends CoreException {
	public function __construct($msg, $code = 0)
	{
		$this->library = 'Module Loader';
		$this->type = 'core';
		$this->generalError = true;
		$this->message = $msg;
		$this->code = $code;
	}
}

/// \class Error404
/// \brief Page not found error.
/// \ingroup exception
///
/// Error raise when the CMS can't display a page.
class Error404 extends CoreException {
	public function __construct()
	{
		$this->library = 'Core System';
		$this->type = 'Page not found';
		$this->generalError = true;
		$this->message = 'Page not found';
		$this->header = 404;
	}
}

/// \class Error403
/// \brief Forbidden error.
/// \ingroup exception
///
/// Error raise when a user try to acces to a page which haven't the right to see.
class Error403 extends ArgError {
	public function __construct()
	{
		parent::__construct();
		$this->library = 'Core System';
		$this->type = 'Forbidden';
		$this->generalError = true;
		$this->message = 'Forbidden';
		$this->header = 403;
	}
}

/// \class ArgError
/// \brief Argument error.
/// \ingroup exception
///
/// Exception raised when they have an error in library calling.
class ArgError extends CoreException {
	public function __construct($msg = '', $code = 0)
	{
		$this->type = 'Argument Exception';
		$this->generalError = true;
		$this->message = $msg;
		$this->code = $code;
		$trace = debug_backtrace();
		if (isset($trace[2]['file']))
			$this->file = $trace[2]['file'];
		if (isset($trace[2]['line']))
		$this->line = $trace[2]['line'];
	}
}
